package com.hc.acoustics.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hc.acoustics.R;
import com.yc.yclibx.adapter.YcAdapterHelper;
import com.yc.yclibx.adapter.YcRecyclerViewAdapter;

import java.util.List;

/**
 *
 */
public abstract class CommonPopupWindow<T> {
    private YcRecyclerViewAdapter<T> mAdapter;
    private PopupWindow mPopupWindow;
    private volatile boolean mIsShow = false;

    public CommonPopupWindow(Context context) {
        this(context, R.layout.weight_popupwindow_details_item);
    }

    public CommonPopupWindow(Context context, @LayoutRes int layoutResId) {
        mAdapter = new YcRecyclerViewAdapter<T>(context, layoutResId) {
            @Override
            public void onUpdate(YcAdapterHelper helper, T item, int position) {
                CommonPopupWindow.this.onUpdate(helper, item, position);
            }
        };
        View contentView = LayoutInflater.from(context).inflate(R.layout.widget_popupwindow, null, false);
        RecyclerView recyclerView = contentView.findViewById(R.id.widgetPopupWindowRv);
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(mAdapter);
        contentView.setOnClickListener(v -> dismiss());
        mPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // 设置PopupWindow是否能响应外部点击事件
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setOnDismissListener(() -> {
            mIsShow = false;
        });
        // 设置PopupWindow是否能响应点击事件
        mPopupWindow.setFocusable(true);
    }

    public void dismiss() {
        mPopupWindow.dismiss();
    }

    public abstract void onUpdate(@NonNull YcAdapterHelper helper, @Nullable T item, int position);

    public void selectItem(YcRecyclerViewAdapter.OnItemClickListener itemClickListener) {
        mAdapter.setItemClickListener(itemClickListener);
    }

    public void addData(List<T> data) {
        mAdapter.addAll(data);
    }

    public void clearData() {
        mAdapter.clear();
    }

    public T getItem(int position) {
        return mAdapter.getItem(position);
    }

    public boolean isShow() {
        return mIsShow;
    }

    public void showAsDropDown(View view) {
        if (mIsShow) {
            mPopupWindow.dismiss();
        } else {
            mIsShow = true;
            mPopupWindow.showAsDropDown(view);
        }
    }
}
